package com.wms.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wms.pojo.Order;
import com.wms.pojo.Wine;
import com.wms.service.OrderService;
import com.wms.service.WineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {


    @Autowired
    private OrderService orderService;

    @Autowired
    private WineService wineService;

    @GetMapping("/getOrders")
    public String getOrders(@RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize,Model model){
        PageHelper.startPage(pageNum,pageSize);
        List<Order> orders = orderService.queryAll();
        PageInfo pageInfo = new PageInfo(orders);
        model.addAttribute("pageInfo",pageInfo);
        return "order/order-list";
    }

    @GetMapping("/toOrder")
    public String toOrder(Model model){
        List<Wine> wines = wineService.queryAll();
        model.addAttribute("wines",wines);
        return "order/order-edit-block";
    }

    @PostMapping("/addOrder")
    public String addOrder(Order order){
        orderService.addOrder(order);
        return "redirect:/order/getOrders";
    }
}
